/*
 * Copyright 2014 François Rajchenbach.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package qmd.rgo.format;

import java.io.BufferedWriter;
import java.io.IOException;

/**
 * A context object returned by the API when the next step is to define the JSON
 * value at the current location.
 * 
 * @param <T>
 *            The type of the context object to be returned after the value is
 *            set
 *
 * @author François Rajchenbach
 * @since 1.0
 */
public class ValueContext<T> {

	private final static CharConverter CONVERTER = new CharConverter();

	private final T nextContext;
	private final BufferedWriter writer;

	protected ValueContext(final T argNextContext,
			final BufferedWriter argWriter) {
		nextContext = argNextContext;
		writer = argWriter;
	}

	/**
	 * Sets the value to <code>null</code>.
	 * 
	 * @return The context object after the value is set
	 * @throws IOException
	 *             If an I/O error occurs
	 */
	public final T valueNull() throws IOException {
		writer.write("null");
		return nextContext;
	}

	/**
	 * Sets the value to the passed boolean argument.
	 * 
	 * @param argValue
	 *            The boolean value to be set
	 * @return The context object after the value is set
	 * @throws IOException
	 *             If an I/O error occurs
	 */
	public final T value(final boolean argValue) throws IOException {
		writer.write(argValue ? "true" : "false");
		return nextContext;
	}

	/**
	 * Sets the value to the passed <code>String<code> argument.
	 * 
	 * @param argValue
	 *            The <code>String</code> value to be set
	 * @return The context object after the value is set
	 * @throws IOException
	 *             If an I/O error occurs
	 */
	public final T value(final String argValue) throws IOException {
		writer.write('"');
		for (char locChar : argValue.toCharArray()) {
			writer.write(CONVERTER.convert(locChar));
		}
		writer.write('"');
		return nextContext;
	}

	/**
	 * Sets the value to the passed numeric argument.
	 * 
	 * @param argValue
	 *            The numeric value to be set
	 * @return The context object after the value is set
	 * @throws IOException
	 *             If an I/O error occurs
	 */
	public final T value(final Number argValue) throws IOException {
		writer.write(argValue.toString());
		return nextContext;
	}

	/**
	 * Sets the value to a new JSON object.
	 * 
	 * @return The context object to set the members of the JSON object
	 * @throws IOException
	 *             If an I/O error occurs
	 */
	public final ObjectContext<T> objectWith() throws IOException {
		writer.write('{');
		return new ObjectContext<T>(nextContext, writer);
	}

	/**
	 * Sets the value to a new JSON array.
	 * 
	 * @return The context object to set the items of the JSON array
	 * @throws IOException
	 *             If an I/O error occurs
	 */
	public final ArrayContext<T> arrayContaining() throws IOException {
		writer.write('[');
		return new ArrayContext<T>(nextContext, writer);
	}

	private static final class CharConverter {

		private final static int MAX = 128;
		private final static String[] RESULTS = new String[MAX];

		static {
			RESULTS['"'] = "\\\"";
			RESULTS['\\'] = "\\\\";
			RESULTS['\b'] = "\\b";
			RESULTS['\f'] = "\\f";
			RESULTS['\n'] = "\\n";
			RESULTS['\r'] = "\\r";
			RESULTS['\t'] = "\\t";
		}

		private String convert(final char argChar) {
			String locResult = null;
			if (argChar < MAX) {
				locResult = RESULTS[argChar];
			}
			return (locResult == null) ? String.valueOf(argChar) : locResult;
		}

	}

}
